#include "calServer.hpp"
#include "memory"
using namespace Server;
static void Usage(std::string proc)
{
    std::cout<<"Usage:\n\t"<<proc<<"serverPort\n\n";
}
//根据传入的req，输出resp
bool cal(const Request& req,Response& resp)
{
    resp._exitCode = OK;
    resp._result = OK;
    switch(req._op)
    {
    case '+':
        resp._result=req._x+req._y;
        break;
    case '-':
        resp._result=req._x-req._y;
        break;
    case '*':
        resp._result=req._x*req._y;
        break;
    case '/':
    {
        if(0==req._y){resp._exitCode=DIV_ZERO_ERR;}
        else
            resp._result=req._x/req._y;
    } 
        break;
    case '%':
    {
        if(0==req._y){resp._exitCode=MOD_ZERO_ERR;}
        else
        resp._result=req._x%req._y;
    }
        break;
    default:
        resp._exitCode=OP_ZERO_ERR;
        return false;
    }
    return true;
}
//./tcpServer local_port
int main(int argc,char* argv[])
{
    if(argc!=2)//判断外部传入的参数是否为2
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port=std::stoi(argv[1]);
    std::unique_ptr<CalServer> tsvr(new CalServer(port));
    tsvr->InitServer();
    tsvr->Start(cal);//传入回调函数
    return 0;
}










